<script setup>
	import { computed, ref } from 'vue';
	import { useCartStore } from '@/store/cart';
	import { useAddressStore } from '@/store/address';
	import { useUserStore } from '@/store/userInfo';
	import { useShopStore } from '@/store/shop';
	// 导入地址store
	const addressStore = useAddressStore( )
	// 导入购物车store
	const cartStore = useCartStore( )
	// 导入用户store
	const userStore = useUserStore( )
	const shopStore = useShopStore( )
	// 导入订单云对象
	const orderApi = uniCloud.importObject( 'order' )
	// 计算是否有sku
	const isSku = computed( ( ) => {
		// 如果有则返回sku的属性，如果没有则返回一个无
		return cartStore.cartData?.sku?.length > 0 ? cartStore.cartData.sku : '无'
	} )
	// 获取支付组件实例
	const pay = ref( null )

	// 点击支付
	const payOrder = async ( ) => {

		// 检查收货地址
		if ( !addressStore.consigneeInfo.addressData ) {
			uni.showToast( {
				title: '请选择收货地址',
				icon: 'none'
			} )
			return
		}

		// 构建订单数据
		const orderData = {
			cart_list: cartStore.cartData,
			address: addressStore.consigneeInfo.addressData,
			recipient: addressStore.consigneeInfo.consigneeName,
			recipientMobile: addressStore.consigneeInfo.telNumber,
			tatalPrice: cartStore.totalPrice,
			num: cartStore.totalCount,
			state: 1
		}

		// 调用云对象创建订单
		const result = await orderApi.add( userStore.userInfo.uid, orderData )
		console.log( result )
		// 调起支付
		const resPay = await pay.value.createOrder( {
			order_no: result.order_no,
			provider: 'wxpay',
			total_fee: cartStore.totalPrice * 100, // 转换为分为单位
			type: 'goods',
			description: '商品订单'
		} )
		console.log( resPay, '购买成功之后' )
	}

	const onSuccess = ( res ) => {
		console.log( '支付成功', res )
		cartStore.clearCart( )
	}
</script>
<template>
	<view class="confimOrder">
		<!-- 收货地址 -->
		<address-item></address-item>
		<!-- 订单信息 -->
		<view class="orderInfo">
			<view class="title">
				<text class="shop-name">{{shopStore.shopInfo.shop_name}}</text>
			</view>
			<view class="content" v-for="(item,index) in cartStore.cartData" :key="index">
				<!-- 产品封面 -->
				<view class="cover">
					<image :src="item.cover_pic" mode="aspectFill"></image>
				</view>
				<!-- 产品信息 -->
				<view class="info">
					<view class="name">
						{{item.name}}
					</view>
					<view class="sku">
						规格：{{isSku}}
					</view>
					<view class="buy-price">
						单价：￥{{item.buy_price}}/份
					</view>
					<view class="num">
						数量：{{item.num}}
					</view>
				</view>
				<!-- 右侧合计总价 -->
				<!-- 		<view class="tatal">
					<up-text style="width: fit-content;flex: none; font-weight: 800;" mode="price"
						:text="cartStore.totalPrice" size="16" color="#495057"></up-text>
				</view> -->
			</view>
			<!-- 底部合计信息总件数和总价 -->
			<view class="tatal-bottom">
				<text style="font-size: 14px;color: #969696; display: block;">共 {{cartStore.totalCount}}
					件</text>
				<up-text style="width: fit-content;flex: none; margin: 0 4px;" text="小计:" size="12"
					color="#495057"></up-text>
				<up-text style="width: fit-content;flex: none; font-weight: 800;" mode="price"
					:text="cartStore.totalPrice" size="16" color="#495057"></up-text>
			</view>
		</view>
		<!-- 底部提交支付 -->
		<view class="bottom">
			<view class="left">
				<up-text style="width: fit-content;flex: none; margin: 0 4px;" text="合计:" size="14"
					color="#495057"></up-text>
				<up-text style="width: fit-content;flex: none;font-weight: 800;" mode="price"
					:text="cartStore.totalPrice" size="16" color="#ed544f"></up-text>
			</view>
			<view class="right">
				<button class="btn" @tap="payOrder">立即支付</button>
			</view>
		</view>
		<!-- 支付组件 -->
		<uni-pay ref="pay" returnUrl="/pages/order/order" @success="onSuccess"></uni-pay>
	</view>
</template>


<style lang="scss" scoped>
	.confimOrder {
		@include pageStyle;
		padding: 24rpx;

		/*地址组件*/
		/*订单信息*/
		.orderInfo {
			padding: 16rpx 24rpx;
			margin-top: 32rpx;
			background-color: #fff;
			border-radius: 24rpx;

			/*商家店名*/
			.title {
				padding-bottom: 16rpx;
				border-bottom: 1px solid $line-color-aux;

				.shop-name {
					@include shopName;
				}
			}

			/*订单产品信息*/
			.content {
				display: flex;
				align-items: flex-start;
				margin-bottom: 16rpx;
				padding-top: 16rpx;
				font-size: 28rpx;

				/*封面图*/
				.cover {
					padding: 8rpx;
					margin-right: 16rpx;
					width: 120rpx;
					height: 120rpx;
					overflow: hidden;
					border-radius: 16rpx;
					background-color: #f7f7f7;

					image {
						border-radius: 16rpx;
					}
				}

				/*产品信息*/
				.info {
					flex: 1;
					color: $text-font-color-3;

					.name {
						font-weight: 800;
						color: $text-font-color-2;
					}

					.sku {
						font-size: 24rpx;
					}

					.buy-price {
						font-size: 24rpx;
					}

					.num {
						font-size: 24rpx;
					}
				}
			}

			/*信息底部的合计信息*/
			.tatal-bottom {
				margin-top: 16rpx;
				display: flex;
				justify-content: flex-end;
				align-items: center;
				width: 100%;
			}
		}

		/*底部功能*/
		.bottom {
			display: flex;
			justify-content: space-between;
			align-items: flex-start;
			position: fixed;
			left: 0;
			bottom: 0;
			padding: 16rpx 24rpx;
			width: 100%;
			height: 164rpx;
			background-color: #fff;
			border-top: 1px solid $line-color-aux;
			z-index: 999;

			.left {
				display: flex;
			}

			.right {
				.btn {
					width: 200rpx;
					height: 80rpx;
					background-color: $brand-theme-color;
					color: #fff;
					border-radius: 50rpx;
					border: none;
				}
			}
		}
	}
</style>